from typing import List

class Solution:
    def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:
        k = len(primes)
        dp = [0] * n
        dp[0] = 1  # 第一个超级丑数是1
        pointers = [0] * k  # 每个质数对应的指针
        
        for i in range(1, n):
            candidates = [primes[j] * dp[pointers[j]] for j in range(k)]
            min_val = min(candidates)
            dp[i] = min_val
            
            # 更新所有产生最小值的指针
            for j in range(k):
                if primes[j] * dp[pointers[j]] == min_val:
                    pointers[j] += 1
        
        return dp[-1]    